package dongdongmovie.controller;

import com.dongdongmovie.pojo.VipUser;
import com.dongdongmovie.service.VipUser_Service;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

@Controller
@RequestMapping("VipUser")
public class VipUser_Controller {

    //注入service
    @DubboReference
    private VipUser_Service vipUser_service;

    //注册
    @ResponseBody
    @RequestMapping("register")
    public int addregister(VipUser vipUser) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        return vipUser_service.add(vipUser);
    }

    //登录
    @ResponseBody
    @RequestMapping("login")
    public int login(VipUser vipUser, HttpServletRequest request) throws UnsupportedEncodingException, NoSuchAlgorithmException {
        int show = vipUser_service.show(vipUser);
        if (show == 2) {
            request.getSession().setAttribute("VipUser", vipUser);
        }
        return show;
    }

    //获取登录态\
    @ResponseBody
    @RequestMapping("denglutai")
    public VipUser denglutai(HttpServletRequest request) {
        VipUser vipUser = (VipUser) request.getSession().getAttribute("VipUser");
        return vipUser;
    }

    //退出登录
    @ResponseBody
    @RequestMapping("tui")
    public void tui(HttpServletRequest request) {
        request.getSession().removeAttribute("VipUser");
    }


}
